package com.cmwa.solution.sys.menu.tag;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.cmwa.solution.sys.menu.entity.MenuVo;
import com.cmwa.solution.utils.Constant;
import com.cmwa.solution.utils.spring.SpringContextUtil;
import com.cmwa.solution.utils.velocity.VelocityEngineTool;

/**
 * 类描述：菜单标签
 */
public class MenuTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	protected Map<Integer, List<MenuVo>> menuMap;// 菜单Map

	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	public int doEndTag() throws JspTagException {
		try {
			JspWriter out = this.pageContext.getOut();
			String menu = (String) this.pageContext.getSession().getAttribute(Constant.solution_MENU_CACHE);
			if (menu != null) {
				out.print(menu);
			} else {
				menu = end();
				this.pageContext.getSession().setAttribute(Constant.solution_MENU_CACHE, menu);
				out.print(menu);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String end() {
		try {
			VelocityEngine velocityEngine = SpringContextUtil.getBean(VelocityEngineTool.class).getVelocityEngine();
			Template t = velocityEngine.getTemplate("menu/template/INDEX_MENU.vm", "UTF-8");
			// 设置初始化数据
			VelocityContext context = new VelocityContext();
			context.put("menuList", buildMenuTree());
			// 设置输出
			StringWriter writer = new StringWriter();
			// 将环境数据转化输出
			t.merge(context, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 构建左边菜单树
	 * @return
	 */
	public List<MenuVo> buildMenuTree() {
		List<MenuVo> menus = new ArrayList<MenuVo>();
		if (menuMap == null) {
			return menus;
		}
		int level = 0;
		List<MenuVo> list = getNotNullList(level);
		for (MenuVo rootMenu : list) {
			menus.add(rootMenu);
			recuNextLevelMenu(rootMenu);
		}
		return menus;
	}

	/**
	 * 递归构建菜单树
	 * 
	 * @param currentMenu
	 */
	public void recuNextLevelMenu(MenuVo currentMenu) {
		String token_code = (String) this.pageContext.getSession().getAttribute("token_code");
		if (currentMenu.hasSubMenu(menuMap)) {
			List<MenuVo> list2 = getNotNullList(currentMenu.getLevel() + 1);
			for (MenuVo nextMenu : list2) {
				if (currentMenu.getMenuId().equals(nextMenu.getParentId())) {
					String menuUrl = nextMenu.getUrl();
					if(!"".equals(menuUrl) && menuUrl != null){
						if(menuUrl.indexOf("service") > -1){
							if(menuUrl.indexOf('?') > -1){
								menuUrl += "&token="+token_code;
							}else{
								menuUrl += "?token="+token_code;
							}
						}
					}
					nextMenu.setUrl(menuUrl);
					currentMenu.addIfAbsent(nextMenu);
					recuNextLevelMenu(nextMenu);
				}
			}
		}
	}

	/**
	 * 得到不为空的list
	 * 
	 * @param level
	 * @return
	 */
	private List<MenuVo> getNotNullList(int level) {
		if (menuMap.get(level) == null) {
			return new ArrayList<MenuVo>(0);
		}
		return menuMap.get(level);
	}

	public void setMenuMap(Map<Integer, List<MenuVo>> menuMap) {
		this.menuMap = menuMap;
	}

}
